/// <reference path="../component/bd_map/BDMap.d.ts"/>
import { observable, action } from 'mobx';
import CaseApi, { HandleStatistics, CaseFlowItem, OvertimeType, OvertimeDataType } from '../api/CaseApi';
import { Case } from '../entity/CaseEntiy';
import {
    HandleStatisticsTitles,
    CaseFlowTitles,
    CaseHandleTitles,
    OverTimeTitles,
    OverTimeStates,
    objectToArray,
    numberSort,
    numberArrCompare,
    genStateStr
} from '../utils/common';

const api = new CaseApi();

interface DataType {
    caseHandleStatistics: string;
    key: number;
}

export interface CaseDataTable {
    title: string;
    info: string;
    key?: number;
}

export interface HotEventDataTable {
    title: string;
    info: number;
    detail?: string;
}

export interface HotEventDetails {
    department: string;
    amount: number;
    rate: string;
}

export interface KPIType {
    title: string;
    total: number;
    kpi: number;
    result?: number;
    children?: KPIType[];
}

export default class CaseStore {
    @observable CaseListCategory: string = '';

    @observable caseList: Case[];

    @observable caseHandleStatistics: DataType[];
    // 工单流程表格数据
    @observable CaseFlowTableList: CaseDataTable[][];

    @observable CaseHandeInfo: CaseDataTable[];

    @observable OvertimeList: CaseDataTable[][];

    @observable UnsatisfyTable: CaseDataTable[][];

    @observable UnsatisfyList: Case[];

    @observable FeedbackInfo: CaseDataTable[];

    @observable KPIRank: CaseDataTable[];

    @observable KPIPreview: CaseDataTable[];

    @observable BasicKPI: KPIType;

    @observable ExtraKPI: KPIType;

    @observable HotEventDataList: HotEventDataTable[];

    @observable HotEventDetails: HotEventDetails[];

    @observable HotTopTenList: CaseDataTable[][];

    @observable MapCaseList: Case[];

    @observable curMassFeature: MassFeature[];

    @observable windowFlag: boolean;

    @action.bound
    getCompletedCaseList() {
        const result: Array<Case> = api.fetchCaseList();
        this.caseList = result;
    }

    @action.bound
    setCaseListCategory(category: string) {
        this.CaseListCategory = category;
    }

    @action.bound
    getCaseHandleStatistics() {
        const result: HandleStatistics = api.fetchHandleStatistics();
        let valueList: string[] = objectToArray(result);
        let dataSource: DataType[] = [];
        HandleStatisticsTitles.forEach((v: string, i: number) => {
            dataSource.push({ caseHandleStatistics: v + '\n:\n' + valueList[i], key: i });
        });
        this.caseHandleStatistics = dataSource;
    }

    @action.bound
    getCaseHandleFlow() {
        const result: CaseFlowItem[] = api.fetchCaseFlow();
        let dataList: CaseDataTable[][] = [];
        result.forEach((v: CaseFlowItem, i: number) => {
            const dataListItem = objectToArray(result[i]);
            dataList.push([]);
            CaseFlowTitles.forEach((val: string, ele: number) => {
                dataList[i].push({ title: val, info: dataListItem[ele], key: ele });
            });
        });
        this.CaseFlowTableList = dataList;
    }

    @action.bound
    getCaseHandleInfo() {
        const result = api.fetchHandleInfo();
        const dataList = objectToArray(result);
        const dataSource: CaseDataTable[] = [];
        CaseHandleTitles.forEach((v: string, i: number) => {
            dataSource.push({ title: v, info: dataList[i], key: i });
        });
        this.CaseHandeInfo = dataSource;
    }

    @action.bound
    getOverTimeList(state: number[]) {
        state = numberSort(state);
        const result: OvertimeType = api.fetchOverTimeList();
        let dataList: OvertimeDataType[] = result.row;
        let states: string[] = OverTimeStates.split('、');
        if (state.length === 0) {
            dataList = dataList;
        } else {
            dataList = dataList.filter((item: OvertimeDataType) => {
                if (numberArrCompare(item.overtimeState, state)) {
                    return item;
                } else {
                    return false;
                }
            });
        }
        let dataSource: CaseDataTable[][] = [];
        dataList.forEach((v: OvertimeDataType, i: number) => {
            const dataListItem = objectToArray(v);
            dataSource.push([]);
            OverTimeTitles.forEach((val: string, index: number) => {
                if (index === 0) {
                    dataListItem[0] = (i + 1) + '、 ' + dataListItem[0];
                }
                if (index === 3) {
                    dataListItem[3] = genStateStr(dataListItem[3], states);
                }
                dataSource[i].push({ title: val, info: dataListItem[index], key: index });
            });
        });
        this.OvertimeList = dataSource;
    }
    @action.bound
    getUnsatisfy() {
        const result = api.fetchUnsatisfyDetails();
        this.UnsatisfyTable = result;
    }
    @action.bound
    getUnsatisfyList(key: number) {
        const container = api.fetchUnsatisfyList();
        let result: Case[] = [];
        if (key === 8) {
            this.UnsatisfyList = container;
            return;
        }
        container.forEach((v: Case, i: number) => {
            if (v.unsatisfyClass === i) {
                result.push(v);
            }
        });

        this.UnsatisfyList = result;
    }
    @action.bound
    getFeedbackInfo() {
        const result = api.fetchFeedbackTable();
        this.FeedbackInfo = result;
    }
    @action.bound
    getKPIRank() {
        const result = api.fetchKPIRank();
        result.forEach((v: CaseDataTable, i: number) => {
            if (i !== 0) {
                v.title = i + '';
            }
        });
        this.KPIRank = result;
    }
    @action.bound
    getKPIPreview() {
        const result = api.fetchKPIPreview();
        this.KPIPreview = result;
    }
    @action.bound
    getBasicKPI() {
        const result = api.fetchBasicKPI();
        this.BasicKPI = result;
    }
    @action.bound
    getExtraKPI() {
        const result = api.fetchExtraKPI();
        this.ExtraKPI = result;
    }
    @action.bound
    getHotEventDataList() {
        const result = api.fetchHotEventDataList();
        this.HotEventDataList = result;
    }
    @action.bound
    getHotEventDetails() {
        const result = api.fetchHotEventDetails();
        this.HotEventDetails = result;
    }

    @action.bound
    getHotTopTenList() {
        const result = api.fetchHotTopTenList();
        this.HotTopTenList = result;
    }
    @action.bound
    getMapCaseList() {
        const result = api.fetchMapCaseList();
        this.MapCaseList = result;
    }
    @action.bound
    setMapMassFeature(data: MassFeature) {
        const result = api.fetchMapCaseList();
        this.curMassFeature = result;
    }
    @action.bound
    openInfoWindow() {
        this.windowFlag = true;
    }
    @action.bound
    closeInfoWindow() {
        this.windowFlag = false;
    }

}